package com.yikome.product.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yikome.product.entity.Category;
import com.yikome.product.service.CategoryService;
import com.itmuch.lightsecurity.annotation.PreAuthorize;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

	@Autowired
	private CategoryService categoryService ; 
	
	@GetMapping("/tree-list")
	public List<Category> findCategoryTreeGrid(){
		
		return categoryService.findCategoryFullTree();
		
	}
	
	@PostMapping
	@PreAuthorize("hasAnyRoles('admin','shop', 'affiliate')")
	public Category peristCategory(@RequestBody Category category) {
		
		return categoryService.saveCategory(category);
	}
	
	@DeleteMapping("/{categoryId}")
	@PreAuthorize("hasAnyRoles('admin','shop')")
	public boolean deleteCategory(@PathVariable("categoryId") Long categoryId) {
		
		categoryService.deleteCategory(categoryId);
		
		return Boolean.TRUE ;
	}
}
